package com.jani.platform.core.ret;

/**
 * 定义接口返回格式
 */
public class Result<T> {

    private int code;

    private T data;

    private String msg;

    public Result(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public Result(int code, T data) {
        this.code = code;
        this.data = data;
    }

    /**
     * 成功通用返回
     * @param data
     * @param msg
     * @param <U>
     * @return
     */
    public static  <U> Result<U> Success(U data,String msg){
        return new Result<U>(0,data,msg);
    }

    public static  <U> Result<U> Success(U data){
        return new Result<U>(0,data,"ok");
    }

    /**
     * 失败返回值
     * @param code
     * @param data
     * @param msg
     * @param <U>
     * @return
     */
    public static  <U> Result<U> Fail(int code,U data,String msg){
        return new Result<U>(code,data,msg);
    }

    public static  <U> Result<U> Fail(U data,String msg){
        return new Result<U>(-1,data,msg);
    }

    public static  <U> Result<U> Fail(U data){
        return new Result<U>(-1,data,"fail");
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
